# -- coding: utf-8 --

import time
# 所有的工具类都应该是类方法


class FileTools:

    @classmethod
    def read_json(cls, path):
        import json5
        with open(path, encoding='utf8') as file:
            contents = json5.load(file)
        return contents

class DBTools:
    @classmethod
    def get_conn(cls):
        import pymysql
        return pymysql.connect(host='192.168.44.128', user='root', password='123456', database='woniuboss4.0')

    @classmethod
    def update(cls, sql):
        conn = cls.get_conn()
        cursor = conn.cursor()
        cursor.execute(sql)
        conn.commit()
        cursor.close()
        conn.close()

    @classmethod
    def query(cls, sql):
        conn = cls.get_conn()
        cursor = conn.cursor()
        cursor.execute(sql)
        result = cursor.fetchall()
        cursor.close()
        conn.close()
        return result


class UITools:
    driver = None

    @classmethod
    def get_driver(cls, browser):
        from selenium import webdriver
        if hasattr(webdriver, browser):
            cls.driver = getattr(webdriver, browser)()
        else:
            cls.driver = webdriver.Firefox()
        cls.driver.implicitly_wait(5)
        cls.driver.maximize_window()
        return cls.driver

    @classmethod
    def get_element(cls, locator):  # locator = {'by':'ID','what':'xxx'}
        """
        根据定位方式来查找元素对象，如果找不到返回None
        :param locator:
        :return:
        """
        by = locator['by']
        what = locator['what']
        try:

            from selenium.webdriver.common.by import By
            return cls.driver.find_element(getattr(By, by), what)
        except Exception as e:  # 没有找到，返回异常转回None
            print(e)
            return None

    @classmethod
    def input(cls, locator, value): # locator = ({'by':'ID','what':'xxx'},'woniu123')
        """
        向文本框元素输入内容
        :param locator: 定位方式
        :param value: 要输入的值
        :return: None
        """
        element = cls.get_element(locator)
        element.click()
        element.clear()
        element.send_keys(value)

    @classmethod
    def click(cls, locator): # locator = {'by':'ID','what':'xxx'}
        """
        点击某个元素
        :param locator:元素定位方式
        :return: None
        """
        element = cls.get_element(locator)
        element.click()

    @classmethod
    def select_option(cls,locator): # locator = {'by':'ID','what':'xxx'}
        """
        随机选择下拉数据某一项但不包括首行
        :param locator:
        :return:
        """
        element = cls.get_element(locator)
        from selenium.webdriver.support.select import Select
        size = len(Select(element).options)
        import random
        random_index = random.randint(1, size - 1)
        Select(element).select_by_index(random_index)


class TestCase:
    driver = UITools.get_driver("Firefox")
    def setup(self):
        driver = self.driver
        driver.get('http://192.168.44.128:8080/WoniuBoss4.0/')
        UITools.input({"by": "NAME", "what": "userName"}, "WNCD000")
        UITools.input({"by": "NAME", "what": "userPass"}, "woniu123")
        UITools.click({"by":"CLASS_NAME","what":"btn.btn-primary.btn-block.btn-save"})

    def test_add_assets(self):
        self.setup()
        driver = self.driver
        locators = FileTools.read_json('locator.json')
        print(locators)

        driver.get('http://192.168.44.128:8080/WoniuBoss4.0/assets')
        UITools.click(locators['add_assets_button']) # 点击新增

        sql = 'SELECT count(assets_id) from assets'
        old = DBTools.query(sql)  # 获得没有添加设置之前的记录数
        # 新增设备名称 # 完成随机选择一项的目标
        UITools.select_option(locators['assets_name'])
        # 选择随机的设备类型
        UITools.select_option(locators['assets_type'])

        # # 条码
        UITools.input(locators['barcode'],'98765789654')

        # # 采购价格
        UITools.input(locators['price'],'100')

        # 归属人
        UITools.input(locators['employee'],'孔银花')
        time.sleep(1)
        UITools.click(locators['employee_select'])


        # 时间
        UITools.click(locators['calendar'])
        UITools.click(locators['time'])

        # 备注
        UITools.input(locators['note'],'说明')

        # 归属
        UITools.select_option(locators['owner'])

        # # 保存
        UITools.click(locators['assets_save'])
        #
        time.sleep(2)
        # 断言
        new = DBTools.query(sql)  # 获得添加设备之后的记录数
        if new[0][0] - old[0][0] == 1:
            actual = '添加成功'
        else:
            actual = '添加失败'
        #
        # 断言：实际结果和期望结果的对比
        if '添加成功' == actual:
            print('断言成功')
        else:
            print('断言失败')

    def test_query_assets(self):
        pass

    def test_modify_assets(self):
        pass


if __name__ == '__main__':
    tc = TestCase()
    tc.test_add_assets()